package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.DesJson.Des3;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.view.CustomDeleteInfoView;

import java.util.HashMap;
import java.util.Map;

/**
 *  重置密码的界面
 *
 * Created by Administrator on 2016/12/8 0008.
 */
public class ChangePwdActivity extends BaseActivity{

    private String phone;
    /*旧密码,新密码,确认密码的条目控件*/
    private CustomDeleteInfoView mOldPwd;
    private CustomDeleteInfoView mNewPwd;
    private CustomDeleteInfoView mConfirmPwd;
    /*提交按钮*/
    private Button mBtnSubmit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        initData();
        //将当前activity添加到集合中
        CommonUtils.addActivity(this);

        setHeader();

        initView();



        setListener();
    }

    private void initData() {
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
    }

    /**
     * 设置头
     */
    private void setHeader() {
        setTitle(R.string.change_password);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //旧密码
        mOldPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_chang_old_pwd);
        //新密码
        mNewPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_change_new_pwd);
        //确认密码
        mConfirmPwd = (CustomDeleteInfoView) findViewById(R.id.cdv_change_password_confirm);

        //提交按钮
        mBtnSubmit = (Button) findViewById(R.id.btn_change_pwd_submit);

        //忘记密码的文本控件
        TextView tvDes = (TextView) findViewById(R.id.tv_change_des);

        //隐藏旧密码和忘记密码控件
        mOldPwd.setVisibility(View.GONE);
        tvDes.setVisibility(View.GONE);

        //设置单行
        mNewPwd.setSingleLine();
        mConfirmPwd.setSingleLine();

        //设置输入类型
        mNewPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        mConfirmPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void setListener() {
        mBtnSubmit.setOnClickListener(new MyOnClickListener());
    }

    private class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_change_pwd_submit:

                    String newPwd = mNewPwd.getText();
                    String confirmPwd = mConfirmPwd.getText();

                    if (Tools.isEmpty(newPwd)){
                        MyToast.show(ChangePwdActivity.this , "请输入密码");
                        return;
                    }

                    if (Tools.isEmpty(confirmPwd)){
                        MyToast.show(ChangePwdActivity.this , "请再次确认密码");
                        return;
                    }

                    if (!newPwd.equals(confirmPwd)){
                        MyToast.show(ChangePwdActivity.this , "两次输入密码不一致");
                        return ;
                    }

                    //检查密码是否符合规范
                    String correctToast = Tools.isCorrectPWD(newPwd, phone);
                    if (!correctToast.equals("true")){
                        MyToast.show(ChangePwdActivity.this , correctToast);
                        return ;
                    }

                    //提交数据到服务器
                    submitData(confirmPwd) ;

                    break;
            }
        }
    }

    /**
     * 提交数据
     * @param confirmPwd 密码
     */
    private void submitData(String confirmPwd) {

        //TODO 需要提交新密码到服务器,暂时直接跳转登录界面
        Map<String,Object> maps = new HashMap<>();
        maps.put("phone",phone);
        maps.put("newpassword", Des3.getEncodePWD(confirmPwd));
        MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_RESET_PWD_URL, maps, MyHttp.POST, ChangePwdActivity.this, false, null, new SimplePaserJsonCallback() {
            @Override
            public void onVerifyIsNull() {
                //跳转到登录界面
                Intent intent = new Intent(ChangePwdActivity.this, LoginActivity.class);
                startActivityForResult(intent, MainActivity.REQUEST_CODE_LOGIN);
                //将集合中的activity关闭掉
                CommonUtils.finishActivity();
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {

            }

            @Override
            public void onErrorCodeError(BaseEntity.MyError mError) {
                super.onErrorCodeError(mError);
                DialogFactory.warningDialog(ChangePwdActivity.this,mError.msg);
            }
        });

    }

    @Override
    public void handleMessage(Message msg) {

    }


    @Override
    public boolean getIsNeedLogin() {
        return false;
    }
}
